package capture
/**
 * Created by IntelliJ IDEA.
 * User: szwl
 * Date: 25.09.11
 * Time: 18:57
 * To change this template use File | Settings | File Templates.
 */



import org.xml.sax.helpers.DefaultHandler
import org.xml.sax.*
import java.net.URLDecoder

class XmlMessageHandler extends DefaultHandler {

    XmlMessageFactory message
    OptionManager options
    boolean visitFlag = false


    XmlMessageHandler() {
        message = XmlMessageFactory.getInstance()
        options = OptionManager.getInstance()
    }

    //actions to take depending on tag name, start tag
    void startElement(String ns, String localName, String qName, Attributes attributes) {
        switch (qName) {
            case 'ping':
                CaptureClient.getInstance().send(message.pong())
                break
            case 'connect':
                options.server = attributes.getValue('server').toDouble()
                break
            case 'process-exclusion':                   //subject - child process, object parent
                if (!options.processListLoaded) {        //skipp server list if local exists, the purpose is to read list from disk in windows/linux environment
                    options.addProcessExclusion(attributes.getValue('excluded'), attributes.getValue('subject'), attributes.getValue('object'))
                }
                break
            case 'file-exclusion':                       //subject process, object
                if (!options.fileListLoaded) {           //overwrite list with one pushed from server
                    options.addFileExclusion(attributes.getValue('excluded'), attributes.getValue('action'), attributes.getValue('object'), attributes.getValue('subject'))
                }
                break
            case 'visit-event':
                visitFlag = true
                options.init = true

                options.program = attributes.getValue('program')
                options.programTimeout = attributes.getValue('time').toInteger()
                options.identifier = attributes.getValue('identifier')
                options.urlMap = [:]
                def url = attributes.getValue('url')
                if (url) {
                    println "${new Date().format('HH:mm:ss')} visit-event start url: ${URLDecoder.decode(attributes.getValue('url'), 'UTF-8')}"
                    options.urlMap[URLDecoder.decode(url)] = 0
                }
                break
            case 'item':
                if (!visitFlag) break
                options.urlMap[URLDecoder.decode(attributes.getValue('url'), 'UTF-8')] = 0
                break
        }
    }

    void characters(char[] chars, int offset, int length) {

    }

    //like above but depending on ending tag
    void endElement(String ns, String localName, String qName) {
        switch (qName) {
            case 'connect':
                def options = OptionManager.getInstance()
                CaptureClient.getInstance().send(message.connect(options.server, options.serverId, options.id))
                break
            case 'visit-event':
                visitFlag = false
                Thread.start {
                    def parser = new StraceLogParser()
                    try {
                        parser.start()
                    }
                    catch (RuntimeException e) {
                        println "$e"
                        e.printStackTrace()
                        CaptureClient.getInstance().run = false
                    }
                }
                CaptureClient.getInstance().send(message.visitEvent(VisitEventType.START))
                break
        }
    }


}
